package com;

import com.fz.entity.User;
import com.fz.entity.UserInfo;
import com.fz.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by webrx on 2018-01-23.
 */

@Slf4j
public class Demo {
    protected SqlSession session = null;

    @Before
    public void init() {
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("conf.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(is);
        this.session = sf.openSession();
    }

    @After
    public void close() {
        session.commit();
        session.close();
    }

    @Test
    public void query() {
        //StudentMapper sdao = session.getMapper(StudentMapper.class);
        //for (Student stu : sdao.query()) {
        //    log.info(stu.toString());
        //}
        UserMapper udao = session.getMapper(UserMapper.class);
        List<User> users = udao.query();
        for (User u : users) {
            //log.info(u.toString());
            log.info(u.getAccount());
            log.info(u.getPass());
            UserInfo info = u.getUserInfo();
            log.info(info == null ? "null" : info.toString());
            log.info("-----------------------------------------");
        }
    }
}

